package org.xinjiang.tarim.travel.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xinjiang.tarim.travel.mapper.AttentionMddMapper;
import org.xinjiang.tarim.travel.mapper.PoiMddMapper;
import org.xinjiang.tarim.travel.mapper.UserMapper;
import org.xinjiang.tarim.travel.model.AttentionMdd;
import org.xinjiang.tarim.travel.model.PoiMdd;
import org.xinjiang.tarim.travel.model.User;
import org.xinjiang.tarim.travel.service.AttentionMddService;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AttentionMddServiceImpl implements AttentionMddService {
    @Autowired
    AttentionMddMapper mapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    PoiMddMapper mddMapper;

    @Override
    public PageBean<AttentionMdd> selectByConditionWithPage(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", (pageBean.getPageIndex()-1) * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        Long totalCount = mapper.selectByConditionCount(criteria);
        List<AttentionMdd> recordList = mapper.selectByCondition(criteria);
        List<Map> resultList = new ArrayList<>();
        for(AttentionMdd record : recordList){
            Map map = new HashMap();
            PoiMdd mdd = mddMapper.selectByMddId(record.getMddId());
            User user = userMapper.selectByPrimaryKey(record.getOpenid());
            map.put("record", record);
            map.put("mdd", mdd);
            map.put("user", user);
            resultList.add(map);
        }
        pageBean.setTotalCount(totalCount);
        pageBean.setResultList(resultList);
        return pageBean;
    }

    @Override
    public List<Map> selectByConditionWithOutPage(Criteria criteria) {
        List<AttentionMdd> recordList = mapper.selectByCondition(criteria);
        List<Map> resultList = new ArrayList<>();
        for(AttentionMdd record : recordList){
            Map map = new HashMap();
            PoiMdd mdd = mddMapper.selectByMddId(record.getMddId());
            User user = userMapper.selectByPrimaryKey(record.getOpenid());
            map.put("record", record);
            map.put("mdd", mdd);
            map.put("user", user);
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public int deleteByPrimaryKey(AttentionMdd record) {
        return mapper.deleteByPrimaryKey(record);
    }

    @Override
    public int deleteByUserId(String openid) {
        return mapper.deleteByUserId(openid);
    }

    @Override
    public int insert(AttentionMdd record) {
        return mapper.insert(record);
    }

    @Override
    public List<AttentionMdd> selectByOpenid(Criteria criteria) {
        return mapper.selectByCondition(criteria);
    }
}
